﻿package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import common.serialization.XmlDataBean;

//xml字符串转换成对象工具类
public class XmlToDataBeanUtil {
	private static XmlToDataBeanUtil xmlToDataBeanUtil = null;

	public static XmlToDataBeanUtil getInstance() throws Exception {
		if (xmlToDataBeanUtil == null) {
			xmlToDataBeanUtil = new XmlToDataBeanUtil();
		}
		return xmlToDataBeanUtil;
	}

	public XmlDataBean readFromFile(String pathname) throws Exception {
		String fileContent = "";
		File f = new File(pathname);

		InputStreamReader isread = null;
		BufferedReader reader = null;
		try {
			isread = new InputStreamReader(new FileInputStream(f), "UTF-8");
			reader = new BufferedReader(isread);
			String line = "";
			while ((line = reader.readLine()) != null) {
				fileContent = fileContent + line;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				isread.close();
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return getXmlDataBean(fileContent);
	}

	public XmlDataBean readFromFile(InputStream is) throws Exception {
		String fileContent = "";

		InputStreamReader isread = null;
		BufferedReader reader = null;
		try {
			isread = new InputStreamReader(is, "UTF-8");
			reader = new BufferedReader(isread);
			String line = "";
			while ((line = reader.readLine()) != null) {
				fileContent = fileContent + line;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				isread.close();
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return getXmlDataBean(fileContent);
	}

	public XmlDataBean getXmlDataBean(String xmlText) throws Exception {
		Document document = DocumentHelper.parseText(xmlText);
		XmlDataBean root = new XmlDataBean();
		setDataBeanNode(document.getRootElement(), root);
		return (XmlDataBean) root.getChildXmlDataBeanList().get(0);
	}

	protected void setDataBeanNode(Element el, XmlDataBean XDB)
			throws Exception {
		XmlDataBean xdb = new XmlDataBean();
		xdb.setNameSpace(el.getName());
		String text = el.getText();
		if (!StringUtil.isNull(text)) {
			xdb.setText(text);
		}
		setDataBeanAttribute(el, xdb);
		XDB.appendChildXmlDataBean(xdb);
		for (Iterator<?> it = el.elementIterator(); it.hasNext();) {
			Element element = (Element) it.next();
			setDataBeanNode(element, xdb);
		}
	}

	protected void setDataBeanAttribute(Element element, XmlDataBean XDB)
			throws Exception {
		for (Iterator<?> it = element.attributeIterator(); it.hasNext();) {
			Attribute attribute = (Attribute) it.next();
			if ("id".equals(attribute.getName())) {
				XDB.setId(attribute.getText());
				continue;
			}
			XDB.setAttribute(attribute.getName(), attribute.getText());

		}
	}

	public static void main(String[] arg) throws Exception {

	}

}